package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.MedicineEntity;
import io.renren.modules.hos.service.MedicineService;
import io.renren.modules.hos.utils.PageParamsUtils;
import io.renren.modules.hos.vo.CollMedicineContentVo;
import io.renren.modules.hos.vo.CollMedicineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.CollMedicineDao;
import io.renren.modules.hos.entity.CollMedicineEntity;
import io.renren.modules.hos.service.CollMedicineService;


@Service("collMedicineService")
public class CollMedicineServiceImpl extends ServiceImpl<CollMedicineDao, CollMedicineEntity> implements CollMedicineService {

    @Autowired
    private MedicineService medicineService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CollMedicineEntity> page = this.page(
                new Query<CollMedicineEntity>().getPage(params),
                new QueryWrapper<CollMedicineEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public CollMedicineContentVo selectMedicineInfoById(Long medicineId) {
        MedicineEntity medicineEntity = medicineService.getById(medicineId);
        return copy(medicineEntity);
    }

    @Override
    public boolean deleteById(Long medicationId) {
        Long hosUserId = ShiroUtils.getHosUserId();
        LambdaQueryWrapper<CollMedicineEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CollMedicineEntity::getMedicineId, medicationId);
        lambdaQueryWrapper.eq(CollMedicineEntity::getUserId, hosUserId);
        remove(lambdaQueryWrapper);
        return true;
    }

    @Override
    public PageUtils queryListByUser(Long hosUserId, Map<String, Object> params) {
        List<CollMedicineVo> collMedicineVoList = baseMapper.selectMedicineInfoByUserId(hosUserId);
        int pageSize = PageParamsUtils.getSize(params);
        int pageIndex = PageParamsUtils.getIndex(params);
        return new PageUtils(collMedicineVoList, collMedicineVoList.size(), pageSize, pageIndex);

    }

    private CollMedicineContentVo copy(MedicineEntity medicineEntity){
        CollMedicineContentVo collMedicineContentVo = new CollMedicineContentVo();
        collMedicineContentVo.setMedicineId(medicineEntity.getMedicineId());
        collMedicineContentVo.setMedicineName(medicineEntity.getMedicineName());
        collMedicineContentVo.setMedicineType(medicineService.selectMedicineType(medicineEntity.getMedicineType()));
        collMedicineContentVo.setMedicineImg(medicineEntity.getMedicineImg());
        collMedicineContentVo.setMedicineDes(medicineEntity.getMedicineDes());
        collMedicineContentVo.setMedicineProductionDate(medicineEntity.getMedicineProductionDate());
        collMedicineContentVo.setMedicineQualityDate(medicineEntity.getMedicineQualityDate());

        return collMedicineContentVo;
    }

}